
const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    bLeft: boolean = false;
    bRight: boolean = false;
    bUp: boolean = false;
    bDown: boolean = false;

    iSpeed: number = 500;

    onLoad () {
        
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.onKeyDown, this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, this.onKeyUp, this);
    }

    start () {

    }

    update (dt: number) {
        if (this.bLeft) {
            this.node.x -= dt*this.iSpeed;
        }
        if (this.bRight) {
            this.node.x += dt*this.iSpeed;
        }
        if (this.bUp) {
            this.node.y += dt*this.iSpeed;
        }
        if (this.bDown) {
            this.node.y -= dt*this.iSpeed;
        }
    }

    onKeyDown(event){
        switch(event.keyCode){
        case cc.macro.KEY.a:
            this.bLeft = true;
            break;
        case cc.macro.KEY.d:
            this.bRight = true;
            break;
        case cc.macro.KEY.w:
            this.bUp = true;
            break;
        case cc.macro.KEY.s:
            this.bDown = true;
            break;
        }
    }

    onKeyUp(event){
        switch(event.keyCode){
        case cc.macro.KEY.a:
            this.bLeft = false;
            break;
        case cc.macro.KEY.d:
            this.bRight = false;
        case cc.macro.KEY.w:
            this.bUp = false;
        case cc.macro.KEY.s:
            this.bDown = false;
            break;
        }
    }
}
